#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QSplitterHandle>
#include <QMessageBox>
#include <QToolBox>
#include <QLabel>
#include "ui/dialog/DialogAddOneGroup.h"
#include "threads/DataRefreshThread.h"
#include "InstrumentSettingWidget.h"


static const int ACTION_ID_START_MENUM   =  0  ;
static const int ACTION_ID_START_TOOLBAR = 100 ;


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    m_pThreadDraw  = DataRefreshThread::Instance()  ;
    ui->setupUi(this);
    //################################
    //ui->splitter->setStyleSheet( "QSplitter::handle { background-color: blue }");
    ui->splitter->setHandleWidth(3) ;
    ui->splitter->setCollapsible(0 , false );
    ui->splitter->setCollapsible(1 , false );
    //################################

    ui->TabWidgetRight->SetHideAble(false) ;
    CreateToolBar()  ;
    CreateStatusBar() ;
    CreateInstrumentSettingWidget();
    connect(ui->TabWidgetLeft , SIGNAL(signalLastTabBottonCliecked(Qt::MouseButton)) , this , SLOT(slotsLeftTabButton(Qt::MouseButton))) ;
    connect(ui->TabWidgetRight , SIGNAL(signalLastTabBottonCliecked(Qt::MouseButton)) , this , SLOT(slotsRightTabButton(Qt::MouseButton))) ;
}

MainWindow::~MainWindow()
{
    if(m_pThreadDraw)
        m_pThreadDraw->terminate();
    delete ui;
}

void MainWindow::resizeEvent(QResizeEvent *)
{
    int _nWidth   = ui->centralwidget->width();
    int _nHeight  = ui->centralwidget->height();
    ui->splitter->setGeometry(0 , 0 , _nWidth , _nHeight);

}

void MainWindow::CreateToolBar()
{
    QToolBar* toolbar = ui->toolBar ;
    toolbar->setStyleSheet("QToolBar { background: cyan; spacing: 3px; }");

    toolbar->setMovable(false);
    setToolButtonStyle( Qt::ToolButtonIconOnly );
    QAction* actions[26] ;

    for(int i = 0 ; i < 26 ; i++)
    {
        QString str = QString(":/icon/toolbar/resource/toobar/0-");
        QString tmp; tmp.sprintf("%02d.png", i+1) ;
        str += tmp ;
        QByteArray array = str.toLatin1() ;
        //qDebug("%s" , array.data());
        actions[i] = new QAction( QPixmap(array.data()), "DPL", toolbar );
        actions[i]->setData(i+ACTION_ID_START_TOOLBAR);
        toolbar->addAction( actions[i] );
    }
    connect(toolbar ,SIGNAL(actionTriggered(QAction*)) , this , SLOT(slotActionTriggered(QAction*))) ;
}

void MainWindow::CreateStatusBar()
{
    QStatusBar* _status = ui->statusbar ;
    _status->setStyleSheet("QStatusBar {background : black;}");
    //################################

    QPalette pal = this->palette() ;


    m_pStatusCell[0] = new QLabel(_status) ;
    pal.setColor(QPalette::Foreground , QColor(255, 255 , 0));
    m_pStatusCell[0]->setPalette(pal);

    m_pStatusCell[0]->setText("Setting Info: GROUP QTY: 1 , LAW QTY: 102 , DATA SIZE: 10250  ");
    m_pStatusCell[1] = new QLabel(_status) ;
    pal.setColor(QPalette::Foreground , QColor(0, 255 , 0));
    m_pStatusCell[1]->setPalette(pal);

    m_pStatusCell[1]->setText("Instrument Info: Baterry: 89% , Temperature: 48 deg  ");
    m_pStatusCell[2] = new QLabel(_status) ;
    pal.setColor(QPalette::Foreground , QColor(0, 255 , 128));
    m_pStatusCell[2]->setPalette(pal);

    m_pStatusCell[2]->setText("Message: Normal ");
    _status->addWidget(m_pStatusCell[0]);
    _status->addWidget(m_pStatusCell[1]);
    _status->addWidget(m_pStatusCell[2] , 1);
}

void MainWindow::UpdateStatusBarMessage(int nId_ , QString& str_)
{
    QLabel* _pCell = m_pStatusCell[nId_]  ;
    _pCell->setText(str_);
}

void MainWindow::CreateInstrumentSettingWidget()
{
    DopplerGroupTab* _pGroup = ui->Group1 ;
    QToolBox* _pToolBox = _pGroup->GetToolBox() ;
    m_pInstrument = new InstrumentSettingWidget(ui->TabWidgetLeft) ;
    _pToolBox->addItem(m_pInstrument, tr("Scanner and Instrument"));
    QIcon icon(":/icon/toolbar/resource/toobar/0-07.png");
    _pToolBox->setItemIcon(6, icon);
}

void MainWindow::slotActionTriggered(QAction* action_)
{
    int _nActionId = action_->data().toInt() ;
    //qDebug("action id %d" , _nActionId);
    if(_nActionId == ACTION_ID_START_TOOLBAR)
    {
        //if(!m_pThreadDraw)m_pThreadDraw = DataRefreshThread::Instance() ;
        bool _bIsRunning = m_pThreadDraw->isRunning() ;
        if(_bIsRunning)
            m_pThreadDraw->StopThread();
        else
            m_pThreadDraw->start();
    }
}


void MainWindow::slotsLeftTabButton(Qt::MouseButton btn_)
{
    if(btn_ == Qt::LeftButton)
    {
        DialogAddOneGroup _dlg(this) ;
        _dlg.exec();
    }
    else if (btn_ == Qt::RightButton)
    {
        qDebug("RightButton LeftTab" );
    }
}


void MainWindow::slotsRightTabButton(Qt::MouseButton btn_)
{
    if(btn_ == Qt::LeftButton)
    {
        QMessageBox messageBox(this);
        messageBox.setText(tr("Add One View ?"));
        QAbstractButton* _pBtnYes = (QAbstractButton*)messageBox.addButton(tr("YES"), QMessageBox::ActionRole);
        QAbstractButton* _pBtnNo  = (QAbstractButton*)messageBox.addButton(tr("NO"), QMessageBox::ActionRole);

        messageBox.exec();
        if (messageBox.clickedButton() == _pBtnYes)
        {
            qDebug("ADD ONE VIEW" );
        }
        else if(messageBox.clickedButton() == _pBtnNo)
        {
            qDebug("CANCEL" );
        }
    }
    else if (btn_ == Qt::RightButton)
    {
        qDebug("RightButton RightTab" );
    }
}
